<?
class ManageBoothMenuAddForm extends Form
{
	var $hotel = array();
	
	function ManageBoothMenuAddForm()
	{
		Form::Form('ManageRoomForm');

		$this->link_css('style/ManageRoom/roomform.css');
		$this->link_css("style/paging.css");
		$this->link_css('style/jquery-ui-1.9.2.custom.css');
		$this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
		
		$this->hotel = Db_Booth::getBoothById(Url::getParamInt("booth_id"));
	}


	function on_submit()
	{
		global $display;
		
		$room['booth_id'] 		= $this->hotel['id'];
		$room['name'] 			= Url::get('name');

        mb_internal_encoding("UTF-8");
        $room['name'] = mb_strtoupper(mb_substr($room['name'],0,1)) . mb_substr($room['name'],1);

		$room['price'] 			= Url::cdouble(Url::get('price'));

		$room['status'] 		= Url::getInt('status');
		$room['desc'] 		= Url::get('desc');
        $room['time_c'] 		= time();
        if (isset($_FILES["img_small"]) && $_FILES["img_small"]["size"] > 0) {
            $store_in = 'images/upload/boothmenu/';
            $name_small = md5($_FILES["img_small"]["tmp_name"]);
            move_uploaded_file($_FILES["img_small"]["tmp_name"], $store_in . $name_small.'.jpg');
            $room['img_small'] =  $store_in .$name_small.'.jpg';
        } else {
            unset($room['img_small']);
        }

        if (isset($_FILES["img_lager"]) && $_FILES["img_lager"]["size"] > 0) {
            $store_in = 'images/upload/boothmenu/';
            $name_lager = md5($_FILES["img_lager"]["name"]);
            move_uploaded_file($_FILES["img_lager"]["tmp_name"], $store_in . $name_lager.'.jpg');
            $room['img_lager'] =  $store_in . $name_lager.'.jpg';
        } else {
            unset($room['img_lager']);
        }

		//$room = HTML::cleanHtml($room);

        $valid = true;
        if($room['name']==''){
            $valid = false;
            $message = "Tên thuc don không được để trống!";
        }
        if($room['desc']==''){
            $valid = false;
            $message = "Mo ta thuc don không được để trống!";
        }
        if($room['price']=='' || $room['price'] ==0){
            $valid = false;
            $message = "gia không được để trống hoac bang 0!";
        }

        if($valid){

            if(Db_BoothMenu::insert($room))
            {
                $this->setFormSucces("success","Save hành công!");
                //Url::redirect_current(array('cmd','hotel_id','success'=>1));
                $href = Url::get('href');
                if($href)
                {
                    Url::redirect_url($href);
                }
                else
                {
                    Url::redirect_current(array('booth_id'));
                }
            }
            else
            {
                $this->setFormError('message', 'Không lưu được phòng.');
            }
        }else{
            $this->setFormError('message', $message);
        }
	}
    function draw()
    {
        global $display;
        $this->beginForm(true);
        $room = array();
        $room['booth_id'] 	= $this->hotel['id'];
        if(Url::get('success') == 1)
        {
            $this->setFormSucces('Insert success', 'Thêm mới thành công.');
            $display->add("sucess_msg", $this->showFormSuccesMessages(1));
        }
        else
        {
            $room['name'] 			= Url::get('name');
            $room['price'] 			= Number::price_fomart(Url::cdouble(Url::get('price')));
            $room['status'] 		= Url::getInt('status',1);


            $display->add("WEB_DIR",WEB_DIR);

            $display->add('hotelName', $this->hotel['name']);

            $status_radios = array(1=>'Hiện', 2=>'Ẩn');

            if($room['status']<0)
            {
                $status_radios[-1] = 'Xóa';
            }

            $display->add('status_radios', 			$status_radios);



            $display->add('alow_edit_hotel', 1);
        }

        if (User::is_admin() || $this->hotel['percent'] == 0){
            $display->add("is_show_fix_price",1);
            $display->add("is_admin",1);
            $room['price_fix'] 	= Url::get('price_fix',0);
        }

        $display->add('room', $room);
        $display->add("message",$this->showFormErrorMessages(1));
        $display->add("success",$this->showFormSuccesMessages(1));
        $display->output('RoomForm');
        $this->endForm();
    }

}